package com.kl.forum.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kl.forum.dao.CircleCommentLightMapper;
import com.kl.forum.dao.CircleCommentMapper;
import com.kl.forum.dao.CircleReplyMapper;
import com.kl.forum.entity.CircleComment;
import com.kl.forum.entity.CircleCommentLight;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CircleCommentService {

    @Autowired
    private CircleCommentMapper circleCommentMapper;
    @Autowired
    private CircleReplyMapper circleReplyMapper;

    @Autowired
    private CircleCommentLightMapper circleCommentLightMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public PageInfo findCircleComment(Integer userId,Integer circleArticleId,Integer pageNum,Integer pageSize,String sort){
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("userId",userId);
        map.put("circleArticleId",circleArticleId);
        map.put("sort",sort);
        List<CircleComment> list=circleCommentMapper.findCircleComment(map);
        PageHelper.clearPage();
        for (int i=0;i<list.size();i++){
//            因为要排序所以没有存到redis
//            if (!stringRedisTemplate.opsForHash().hasKey("circleArticleCommentLightNumCount",list.get(i).getId().toString())){
//                stringRedisTemplate.opsForHash().put("circleArticleCommentLightNumCount",list.get(i).getId().toString(),"0");
//            }
//            Object lightNumCount=stringRedisTemplate.opsForHash().get("circleArticleCommentLightNumCount",list.get(i).getId().toString());
//            list.get(i).setLightNum(Integer.parseInt(lightNumCount.toString()));
            if (userId!=null){
                CircleCommentLight circleCommentLight= circleCommentLightMapper.findOneCircleCommentLight(userId,list.get(i).getId());
                if (circleCommentLight!=null){
                    list.get(i).setLightStatus(circleCommentLight.getStatus());
                }else {
                    list.get(i).setLightStatus(0);
                }

            }
            map.put("commentId",list.get(i).getId());
            PageHelper.startPage(1, 5);
            List<Map> list1=circleReplyMapper.findCircleCommentReply(map);
            PageInfo<Map> pageInfo = new PageInfo<>(list1);
            PageHelper.clearPage();
            list.get(i).setPageInfoReply(pageInfo);
        }
        PageInfo<CircleComment> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }
    public int insertCircleComment(Integer userId, Integer circleArticleId, String content,Date date){
        CircleComment comment=new CircleComment();
        comment.setUserId(userId);
        comment.setContent(content);
        comment.setIsDel(1);
        comment.setCircleArticleId(circleArticleId);
        comment.setCreateTime(date);
        return circleCommentMapper.insertSelective(comment);
    }


    public int delCircleComment(Integer commentId) {

        return  circleCommentMapper.deleteByPrimaryKey(commentId);
    }
    public  CircleComment findOneCircleCommentBykey(Integer id){
      return   circleCommentMapper.selectByPrimaryKey(id);
    }

    public int update(CircleComment circleComment){
       return circleCommentMapper.updateByPrimaryKeySelective(circleComment);
    }
}
